#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>

int main()
{
  pid_t id = fork();
  if(id < 0)
  {
    perror("fork");
    return 1;
  }
  else if(id == 0)
  {
    //child
    int cnt = 5;
    while(cnt)
    {
      printf("i am a child, pid: %d, ppid: %d, cnt: %d\n", getpid(), getppid(), cnt);
      cnt--;
      sleep(1);
    }
    exit(0);
  }
  else{
    //parent
      int cnt = 10;
      while(cnt)
      {

      printf("i am a parent, pid: %d, ppid: %d\n", getpid(), getppid());
      cnt--;
      sleep(1);
      }
      pid_t ret = wait(NULL);
      if(ret == id)
      {
        printf("wait success! ret: %d\n", ret);
      }
      sleep(5);
  }


  return 0;
}
